package controller;

import com.google.gson.Gson;
import model.Buildlog;
import services.BuildlogService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@WebServlet("/user")//访问接口的名字
public class BuildlogController extends HttpServlet {//继承
    SimpleDateFormat fom = new SimpleDateFormat("yyyy-mm-dd");
    private static Gson gson = new Gson();//第三方插件，返回gson格式的插件

    @Override//重写
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//请求 响应  针对的是前端页面的get方法
		//用户所做的操作
		resp.setContentType("text/html;charset=utf-8");
		String oper = req.getParameter("oper");//获取地址栏当中oper后边的值   请求对象实例化
		System.out.println("获取地址栏当中的数据为：" + oper);//能否获取地址栏中的值
		//用户表的数据

//		System.out.println("获取地址栏当中的数据为："+req.getParameter("name"));//能否获取地址栏中name的值
		/*String buildingsite=req.getParameter("buildingsite");
		String begintime=req.getParameter("begintime");
		String endtime=req.getParameter("endtime");*/
//		String projectprincipal=req.getParameter("projectprincipal");
		/*String price=req.getParameter("price");
		String constructionorganization=req.getParameter("constructionorganization");
		String constructionorganizationcontacts=req.getParameter("constructionorganizationcontacts");
		String accessory=req.getParameter("accessory");
		String replenishaccessory=req.getParameter("replenishaccessory");
		String replenishreason=req.getParameter("replenishreason");
		String isagree=req.getParameter("isagree");
		String replenishtime=req.getParameter("replenishtime");*/

		Buildlog buildlog = new Buildlog();////buildlog工程表实例化
//		buildlog.setProjectprincipal(projectprincipal);//调用set方法拿到给项目负责人设置的值


		List<Buildlog> tempReturn1 = null;//返回集合操作结果  （查询时候使用）没有new的还要进行实例化


		/*System.out.println("获取地址栏当中的数据为："+buildingsite);
		System.out.println("获取地址栏当中的数据为："+begintime);
		System.out.println("获取地址栏当中的数据为："+endtime);*/
//		System.out.println("获取地址栏当中的数据为："+projectprincipal);
		/*System.out.println("获取地址栏当中的数据为："+price);
		System.out.println("获取地址栏当中的数据为："+constructionorganization);
		System.out.println("获取地址栏当中的数据为："+constructionorganizationcontacts);
		System.out.println("获取地址栏当中的数据为："+accessory);
		System.out.println("获取地址栏当中的数据为："+replenishaccessory);
		System.out.println("获取地址栏当中的数据为："+replenishreason);
		System.out.println("获取地址栏当中的数据为："+isagree);
		System.out.println("获取地址栏当中的数据为："+replenishtime);*/

		int tempReturn = 0;//返回int操作结果  （增删改时使用）
		BuildlogService buildlogService = new BuildlogService();//实例化业务功能类

		System.out.println("您要做的操作是（1：单项查询/2:增加/3:修改/4:删除/）" + oper);
		if (oper != null) {
			if (req.getParameter("buildlogid") != null) {
				String buildlogid = req.getParameter("buildlogid");
				System.out.println("获得地址栏中表的数据为" + buildlogid);
			}
			if (req.getParameter("projectprincipal") != null) {
				String projectprincipal = req.getParameter("projectprincipal");
				System.out.println("获得地址栏中表的数据为" + projectprincipal);
				buildlog.setProjectprincipal(projectprincipal);
			}
			if (req.getParameter("price") != null) {
				String price = req.getParameter("price");
				System.out.println("获得地址栏中表的数据为" + price);
				buildlog.setPrice(price);
			}
			if (req.getParameter("constructionorganization") != null) {
				String constructionorganization = req.getParameter("constructionorganization");
				System.out.println("获得地址栏中表的数据为" + constructionorganization);
				buildlog.setConstructionorganization(constructionorganization);
			}

			if (req.getParameter("constructionorganizationcontacts") != null) {
				String constructionorganizationcontacts = req.getParameter("constructionorganizationcontacts");
				System.out.println("获得地址栏中表的数据为" + constructionorganizationcontacts);
				buildlog.setConstructionorganizationcontacts(constructionorganizationcontacts);
			}
			if (req.getParameter("accessory") != null) {
				String accessory = req.getParameter("accessory");
				System.out.println("获得地址栏中表的数据为" + accessory);
				buildlog.setAccessory(accessory);
			}
			if (req.getParameter("replenishaccessory") != null) {
				String replenishaccessory = req.getParameter("replenishaccessory");
				System.out.println("获得地址栏中表的数据为" + replenishaccessory);
				buildlog.setReplenishaccessory(replenishaccessory);
			}
			if (req.getParameter("replenishreason") != null) {
				String replenishreason = req.getParameter("replenishreason");
				System.out.println("获得地址栏中表的数据为" + replenishreason);
				buildlog.setReplenishreason(replenishreason);
			}
			if (req.getParameter("isagree") != null) {
				String isagree = req.getParameter("isagree");
				System.out.println("获得地址栏中表的数据为" + isagree);
				buildlog.setIsagree(isagree);
			}
			if (req.getParameter("replenishtime") != null) {
				String replenishtime = req.getParameter("replenishtime");
				System.out.println("获得地址栏中表的数据为" + replenishtime);
				buildlog.setReplenishtime(replenishtime);
			}
			if (req.getParameter("buildingsite") != null) {
				String buildingsite = req.getParameter("buildingsite");
				System.out.println("获得地址栏中表的数据为" + buildingsite);
				buildlog.setBuildingsite(buildingsite);
			}
			if (req.getParameter("begintime") != null) {
				String begintime = req.getParameter("begintime");
				System.out.println("获得地址栏中表的数据为" + begintime);
				buildlog.setBegintime(begintime);
			}
			if (req.getParameter("endtime") != null) {
				String endtime = req.getParameter("endtime");
				System.out.println("获得地址栏中表的数据为" + endtime);
				buildlog.setEndtime(endtime);
			}
			if (req.getParameter("agreetime") != null) {
				String agreetime = req.getParameter("agreetime");
				System.out.println("获得地址栏中表的数据为" + agreetime);
				buildlog.setAgreetime(agreetime);
			}
			if (req.getParameter("changemoney") != null) {
				String changemoney = req.getParameter("changemoney");
				System.out.println("获得地址栏中表的数据为" + changemoney);
				buildlog.setChangemoney(changemoney);
			}
			if (req.getParameter("lastendtime") != null) {
				String lastendtime = req.getParameter("lastendtime");
				System.out.println("获得地址栏中表的数据为" + lastendtime);
				buildlog.setLastendtime(lastendtime);
			}
			if (req.getParameter("agreedoc") != null) {
				String agreedoc = req.getParameter("agreedoc");
				System.out.println("获得地址栏中表的数据为" + agreedoc);
				buildlog.setAgreedoc(agreedoc);
			}
			if (req.getParameter("state") != null) {
				String state = req.getParameter("state");
				System.out.println("获得地址栏中表的数据为" + state);
				buildlog.setState(state);
			}
			if (req.getParameter("statedetail") != null) {
				String statedetail = req.getParameter("statedetail");
				System.out.println("获得地址栏中表的数据为" + statedetail);
				buildlog.setStatedetail(statedetail);
			}
			if (req.getParameter("ischange") != null) {
				String ischange = req.getParameter("ischange");
				System.out.println("获得地址栏中表的数据为" + ischange);
				buildlog.setIschange(ischange);
			}
			if (req.getParameter("safepollingdoc") != null) {
				String safepollingdoc = req.getParameter("safepollingdoc");
				System.out.println("获得地址栏中表的数据为" + safepollingdoc);
				buildlog.setSafepollingdoc(safepollingdoc);
			}
			if (req.getParameter("changereason") != null) {
				String changereason = req.getParameter("changereason");
				System.out.println("获得地址栏中表的数据为" + changereason);
				buildlog.setChangereason(changereason);
			}
			if (req.getParameter("changeendtime") != null) {
				String changeendtime = req.getParameter("changeendtime");
				System.out.println("获得地址栏中表的数据为" + changeendtime);
				buildlog.setChangeendtime(changeendtime);
			}
			if (req.getParameter("checktime") != null) {
				String checktime = req.getParameter("checktime");
				System.out.println("获得地址栏中表的数据为" + checktime);
				buildlog.setChecktime(checktime);
			}
			if (req.getParameter("checker") != null) {
				String checker = req.getParameter("checker");
				System.out.println("获得地址栏中表的数据为" + checker);
				buildlog.setChecker(checker);
			}
			if (req.getParameter("checkmaterials") != null) {
				String checkmaterials = req.getParameter("checkmaterials");
				System.out.println("获得地址栏中表的数据为" + checkmaterials);
				buildlog.setCheckmaterials(checkmaterials);
			}


			/*if(oper!=null){//判断地址栏输入的oper不等于空    选择要做的操作*/

			//操作的内容
			switch (oper) {
				case "1":
					//查询
					//单项查询
					tempReturn1 = buildlogService.getDan(buildlog);//调用service层的getall方法 返回查询结果

					break;
				case "2":
					//添加
					tempReturn = buildlogService.add(buildlog);//调用service层的add方法  返回添加结果
					if (tempReturn == 0) {
						System.out.println("失败" + tempReturn);
					} else {
						System.out.println("成功" + tempReturn);
					}
					break;
				case "3":
					//修改
					String buildlogid = req.getParameter("buildlogid");//主键  修改时必须获取表中主键（地址栏中必须有该参数）
					buildlog.setBuildlogid(Integer.parseInt(buildlogid));//强制类型转换  给实体主键赋值
					String buildingsite = req.getParameter("buildingsite");//主键  修改时必须获取表中主键（地址栏中必须有该参数）
					buildlog.setBuildingsite(buildingsite);
					String projectprincipal = req.getParameter("projectprincipal");//主键  修改时必须获取表中主键（地址栏中必须有该参数）
					buildlog.setProjectprincipal(projectprincipal);
					System.out.println(projectprincipal);
					tempReturn = buildlogService.update(buildlog);//调用service层的update方法  返回修改结果
					break;
				case "4":
					//删除
					String buildlogid1 = req.getParameter("buildlogid");//主键  删除时必须获取表中主键（地址栏中必须有该参数）
					if (buildlogid1 != null) {
						buildlog.setBuildlogid(Integer.parseInt(buildlogid1));//强制类型转换  给实体主键赋值
						System.out.println(buildlogid1);
						tempReturn = buildlogService.del(buildlog);//调用service层的del方法  返回删除结果

					}
					break;
				case "5":
					//查询全部
					tempReturn1 = buildlogService.getAll(buildlog);//调用service层的getall方法 返回查询结果
					break;
				case "6":
					//更改方法按id查询
					String buildlogid2 = req.getParameter("buildlogid");//主键  修改时必须获取表中主键（地址栏中必须有该参数）
					buildlog.setBuildlogid(Integer.parseInt(buildlogid2));//强制类型转换  给实体主键赋值
					tempReturn1 = buildlogService.getAddc(buildlog);//调用service层的getall方法 返回查询结果
					break;

			}
			//设置返回信息

			//将所有的返回值都写入到map当中
			Map map = new HashMap();//
			map.put("code", "1");//返回编号：1：成功；0：失败；-1：项目负责人重复
			map.put("statue", "success");
			System.out.println(tempReturn1);
			if (tempReturn1 != null) {
				map.put("results", tempReturn1);//成功的结果 操作的结果
			} else {
				map.put("results", tempReturn);
			}

			String rs = gson.toJson(map);
			//将数据发送后并进行打印
			System.out.println(rs+"---------"+map);
			resp.getWriter().print(rs);
		}
	}

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//请求  响应  针对的是前端页面的post方法
        super.doGet(req, resp);//调用父类方法
    }

}